﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Glucometer.ModuleHandler;
using Glucometer.Utility;

namespace Glucometer
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 

        public static DeviceMemory memory = new DeviceMemory();

        public static DeviceSettings setting = new DeviceSettings(2000);

        public static Battery battery = new Battery(Battery.MAX_BATTERY_LIFE);
       
        [STAThread]
        static void Main()
        {

            GlucometerModuleHandler.LoadState(setting,memory);
            
            DateTime dt = new DateTime(2000, 2, 28);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //LOAD STORED MEMORY
            XMLFileDumper.GetDeviceMemoryFromXMLFile(Program.memory);

            //LOAD STORED SETTINGS
            DeviceSettings set =  XMLFileDumper.GetSettingsFromXMLFile();
            if (set != null)
                setting = set;

            Glucometer.UserInterface.GlucometerDeviceHardware hardware = new Glucometer.UserInterface.GlucometerDeviceHardware();//new GlucometerHardware();

            Program.battery.BatteryLife = XMLFileDumper.GetBatteryLifeFromXMLFile();

            HardWareInterruptInterpreter interruptProcessor = new HardWareInterruptInterpreter(hardware, new ButtonClickInterpreter(), Program.battery);

            DeviceStatus status = new DeviceStatus();

            Dictionary<OperationMode, GlucometerModuleHandler> moduleHanlderList = new Dictionary<OperationMode,GlucometerModuleHandler>();

            moduleHanlderList.Add(OperationMode.TESTING, new TestingModuleHandler(hardware, Program.memory,Program.setting, status));
            
            moduleHanlderList.Add(OperationMode.BROWSING, new BrowsingModuleHandler(hardware, Program.memory,Program.setting, status));
            
            moduleHanlderList.Add(OperationMode.SETUP, new SetupModuleHandler(hardware, Program.memory,Program.setting, status));
            
            moduleHanlderList.Add(OperationMode.UPLOADING, new UploadingModuleHandler(hardware, Program.memory,Program.setting, status));
            
            GlucometerController controller = new GlucometerController(
                                                        hardware, 
                                                        interruptProcessor, 
                                                        Program.setting, 
                                                        Program.memory,
                                                        status,
                                                        battery,
                                                        moduleHanlderList
                                                        );

            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
       
            Application.Run(hardware);

        }

        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            GlucometerModuleHandler.PersistState(battery.BatteryLife);
        }
    }
}
